package cn.topcodes.tcsf.admin.web.controller.impl;

import cn.topcodes.tcsf.admin.commons.api.domain.ApiResponseEntity;
import cn.topcodes.tcsf.admin.service.licence.Licence;
import cn.topcodes.tcsf.admin.service.licence.LicenceException;
import cn.topcodes.tcsf.admin.service.licence.LicenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Unicorn
 * @date 2017/5/10
 */
@Controller
@RequestMapping("/admin/licence")
public class LicenceAdminController {

    @Autowired
    private LicenceService licenceService;

    @RequestMapping(value = "",method = RequestMethod.GET)
    public String index(Model model) {
        model.addAttribute("machineCode",licenceService.getMatchineCode());
        Licence licence = licenceService.getLicence();
        model.addAttribute("licence",licence);
        return "admin/licence/index";
    }

    /*系统注册*/
    @ResponseBody
    @RequestMapping(value = "register",method = RequestMethod.POST)
    public ApiResponseEntity register(@RequestParam(required = true) String code) {
        try {
            this.licenceService.register(code);
            return ApiResponseEntity.buildOK();
        } catch (LicenceException e) {
            return ApiResponseEntity.buildFailed(e.getMessage());
        }
    }
}
